//
//  iPickerViewController.m
//  iPicker
//
//  Created by Admin on 17-12-10.
//  Copyright 2010 Bedrijf. All rights reserved.
//

#import "iPickerViewController.h"
#import "SelectionViewController.h";
#import "PeriodDetailsViewController.h";
#import "RegisterViewController.h";
#import "RegisterReportViewController.h";
#import "Selection.h";
#import "Database.h";

@implementation iPickerViewController

@synthesize picker;
@synthesize yearPeriods;
@synthesize years;
@synthesize periods;

- (NSInteger) getYearRow
{
	NSInteger yearRow = [picker selectedRowInComponent:kYearComponent];
	IdValue *year = [years objectAtIndex:yearRow];
	
	return year.id;
}

- (NSInteger) getPeriodRow
{
	NSInteger periodRow = [picker selectedRowInComponent:kPeriodComponent];
	IdValue *period = [periods objectAtIndex:periodRow];
	
	return period.id;
}

- (IBAction) buttonPressed:(id) sender 
{
	PeriodDetailsViewController *detailsViewController = [[PeriodDetailsViewController alloc] initWithNibName:@"DetailsView" bundle:nil];
	
	NSInteger year = [self getYearRow];
	NSInteger period = [self getPeriodRow];
	
	Selection *selection = [[[Selection alloc] init] autorelease];
		
	// get de DBAccess Object
	Database *database = [[Database alloc] init];
	
	//get the selection array from the database
	selection = [database getSelectedPeriodForYear:year :period];
	
	//close the database
	[database closeDatabase];
		
	//release the database to free memory
	[database release];
		
	//set the labels for selected period
	[detailsViewController setLabelsForPeriod:selection];
		
	[self.view addSubview:detailsViewController.view];
}

- (IBAction) registerButton:(id) sender
{
	NSInteger year = [self getYearRow];
	NSInteger period = [self getPeriodRow];
	
	RegisterViewController *registerView = [[RegisterViewController alloc] initWithNibName:@"RegisterView" bundle:nil];
	[registerView setYearAndPeriod:year :period];
	
	[self.view addSubview:registerView.view];
}

-(IBAction) registerReportButton:(id) sender
{
	RegisterReportViewController *registerReportView = [[RegisterReportViewController alloc] initWithNibName:@"RegisterReportView" bundle:nil];
	[self.view addSubview:registerReportView.view];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	Database *database = [[Database alloc] init];
	
	NSMutableArray *yearsArray = [[NSMutableArray alloc] init];
	NSMutableArray *periodsArray = [[NSMutableArray alloc] init];

	yearsArray = [database getYears];
	periodsArray = [database getPeriods];
	
	NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
		
	for (int i = 0; i < [yearsArray count]; i++) {
	
		[dictionary setObject:periodsArray forKey:[yearsArray objectAtIndex:i]];
	}
		
	NSLog(@"dictionary: %@", dictionary);
		
	self.yearPeriods = dictionary;
	[dictionary release];
	[yearsArray release];
	[periodsArray release];
	
	NSArray *components = [self.yearPeriods allKeys];
	NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
	self.years = sorted;
	
	NSString *selectedYear = [self.years objectAtIndex:0];
	NSArray *array = [yearPeriods objectForKey:selectedYear];
	self.periods = array;	
    [super viewDidLoad];
}




// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[picker release];
	[yearPeriods release];
	[years release];
	[periods release];
    [super dealloc];
}

#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 2;	
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	if(component == kYearComponent)
		return [self.years count];
	return [self.periods count];
}

#pragma mark Picker delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	if(component == kYearComponent)
		return [self.years objectAtIndex:row];
	return [self.periods objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	if(component == kYearComponent)
	{
		NSString *selectedYear = [self.years objectAtIndex:row];
		NSArray *array = [yearPeriods objectForKey:selectedYear];
		self.periods = array;
		[picker selectRow:0 inComponent:kPeriodComponent animated:YES];
		[picker reloadComponent:kPeriodComponent];
	}
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
	if(component == kPeriodComponent)
		return 200;
	return 90;
}
@end
